package #Package(${basePackage} "service.impl");


import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.springframework.stereotype.Service;
import #Package(${basePackage} "entity.${EntityName}");
import #Package(${basePackage} "mapper.${EntityName}Mapper");
import #Package(${basePackage} "service.${EntityName}Service");
import #Package(${basePackage} "domain.request.dto.${EntityName}Dto");

import com.tongtu.cyber.common.api.vo.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *    $table.tableComment
 * </p>
 *
 * @author ${author}
 * @date ${date}
 * @remark ${remark}
 */
@Slf4j
@Service
public class ${EntityName}ServiceImpl extends ServiceImpl<${EntityName}Mapper, ${EntityName}> implements ${EntityName}Service {


    @Override
    public Result pageList(${EntityName}Dto.PageListParam pageListParam) {
        if (pageListParam.getPageNo() == null){
            pageListParam.setPageNo(1);
        }

        if (pageListParam.getPageSize() == null){
            pageListParam.setPageSize(10);
        }


        QueryWrapper<${EntityName}> queryWrapper = new QueryWrapper<>();

#foreach($column in ${table.columnInfos})
#if($column.checked && $column.queryChecked && $column.javaFieldName != "id" && $!commonColumns.contains($column.javaFieldName) == false )
#set($type = "#SplitAndGetLastOne($column.javaFieldType '.')" )
#if( $type == "Date")
        if (pageListParam.get#UpperCase(${column.javaFieldName})Start() != null){
            Date timeStart = pageListParam.get#UpperCase(${column.javaFieldName})Start();
            timeStart.setHours(0);
            timeStart.setMinutes(0);
            timeStart.setSeconds(0);
            queryWrapper.ge( "${column.columnName}",  timeStart);
        }

        if (pageListParam.get#UpperCase(${column.javaFieldName})End() != null){
            Date timeEnd = pageListParam.get#UpperCase(${column.javaFieldName})End();
            timeEnd.setHours(23);
            timeEnd.setMinutes(59);
            timeEnd.setSeconds(59);
            queryWrapper.le( "${column.columnName}",  timeEnd);
        }
#elseif($type == "String")
        queryWrapper.like(StringUtils.isNotBlank(pageListParam.get#UpperCase(${column.javaFieldName})()), "${column.columnName}",  pageListParam.get#UpperCase(${column.javaFieldName})());

#else
        queryWrapper.eq(pageListParam.get#UpperCase(${column.javaFieldName})() != null, "${column.columnName}",  pageListParam.get#UpperCase(${column.javaFieldName})());
#end
#end
#end


        queryWrapper.orderByDesc("update_time");
        IPage<${EntityName}> resultPage = baseMapper.selectPage(new Page<>(pageListParam.getPageNo(), pageListParam.getPageSize()),  queryWrapper);
        return Result.ok(resultPage);
    }

    @Override
    public Result detail(${EntityName}Dto.DetailParam detailParam){
        ${EntityName} data = baseMapper.selectById(detailParam.getId());
        return Result.ok(data);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result add(${EntityName}Dto.AddParam addParam){
        Date now = new Date();
        ${EntityName} toUpdate = new ${EntityName}();
        BeanUtils.copyProperties(addParam, toUpdate);

        toUpdate.setId(IdWorker.getId());
        toUpdate.setCreateTime(now);
        toUpdate.setUpdateTime(now);
        toUpdate.setVersion(1);
        toUpdate.setDeleteFlag(0);
        baseMapper.insert(toUpdate);

        return Result.ok(toUpdate);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result update(${EntityName}Dto.UpdateParam updateParam){
        ${EntityName} data = baseMapper.selectById(updateParam.getId());
        if (data == null){
            return Result.error(420,"数据不存在!");
        }

        Date now = new Date();
        ${EntityName} toUpdate = new ${EntityName}();
        BeanUtils.copyProperties(updateParam, toUpdate);

        toUpdate.setId(updateParam.getId());
        toUpdate.setUpdateTime(now);
        if(data.getVersion() == null){
            toUpdate.setVersion(1);
        } else{
            toUpdate.setVersion(data.getVersion() + 1 );
        }

        baseMapper.updateById(toUpdate);

        return Result.ok("修改成功!");
    }



    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result softDelete(${EntityName}Dto.DeleteParam deleteParam){
        ${EntityName} data = baseMapper.selectById(deleteParam.getId());
        if (data == null){
            return Result.error(420,"数据不存在!");
        }

        Date now = new Date();
        ${EntityName} toUpdate = new ${EntityName}();

        toUpdate.setId(deleteParam.getId());
        toUpdate.setDeleteFlag(1);
        toUpdate.setUpdateTime(now);
        if(data.getVersion() == null){
            toUpdate.setVersion(1);
        } else{
            toUpdate.setVersion(data.getVersion() + 1 );
        }
        baseMapper.updateById(toUpdate);

        return Result.ok("删除成功!");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result batchSoftDelete(${EntityName}Dto.BatchDeleteParam batchDeleteParam){
        for (Long id: batchDeleteParam.getIds()){
            Date now = new Date();
            ${EntityName} toUpdate = new ${EntityName}();

            toUpdate.setId(id);
            toUpdate.setDeleteFlag(1);
            toUpdate.setUpdateTime(now);
            baseMapper.updateById(toUpdate);
        }
        return Result.ok("删除成功!");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result hardDelete(${EntityName}Dto.DeleteParam deleteParam){
        baseMapper.deleteById(deleteParam.getId());
        return Result.ok("删除成功!");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result batchHardDelete(${EntityName}Dto.BatchDeleteParam batchDeleteParam){
        for (Long id: batchDeleteParam.getIds()){
            baseMapper.deleteById(id);
        }
        return Result.ok("删除成功!");
    }


}

